<template>
    <div class="container">
        <h1>登录</h1>
        <input placeholder="用户名" v-model="userName"/>
        <input placeholder="密码" type="password" v-model="pwd"/>
        <button @click="login">登录</button>
        <!-- <router-link to="/register" class="router-link">注册</router-link> -->
        <!-- <router-link to="/changePwd" class="router-link">修改密码</router-link> -->
    </div>
</template>

<script>
import axios from 'axios';
import '../../assets/style.css';
import store from '../../store';

export default{
    data() {
        return {
            userName: '',
            pwd: ''
        };
    },
    methods: {
        login() {
            axios.post('http://localhost:8080/shopping/user/login', {
                userName: this.userName,
                pwd: this.pwd
            }).then(response => {
                if (response.data.code === 200) {
                    // 缓存登录用户数据
                    const token = response.data.data;
                    localStorage.setItem('authToken', JSON.stringify(token)); // 或 sessionStorage
                    store.dispatch('login', response.data.data);
                    this.$router.push('/home');
                } else {
                    this.$message.error(response.data.msg);
                }
            }).catch(error => {
                this.$message.error(error)
            });
        }
    }
};
</script>